#include <stdio.h>
#include <string.h>
#include <math.h>

char str[10001] = {0};

unsigned int max(unsigned int a, unsigned int b)
{
	if (a > b)
		return a;
	else
		return b;
}

unsigned int min(unsigned int a, unsigned int b)
{
	if (a < b)
		return a;
	else
		return b;
}
char * addBinary(char * a, char * b)
{
	unsigned int aLen, bLen;
	aLen =	strlen(a);
	bLen =	strlen(b);
//	printf("%d,%d\n", aLen, bLen);
	int LenTotal;
	LenTotal  = max(aLen, bLen) + 1;
//	 char str1[LenTotal];
	unsigned int LenMin;
	LenMin = min(aLen, bLen);
	int d1, d2, carry = 0; //二进制数1，2和进位
	for (unsigned int i = 1; i <= LenMin; i++)
	{
		d1 = a[LenMin - i] - '0';
		d2 = b[LenMin - i] - '0';
		char aBit;
		if (d1 + d2 + carry >= 2)
		{
			//有进位
			aBit = d1 + d2 + carry - 2  + '0';
			carry = 1;
		}
		else
		{
			aBit = d1 + d2 + carry  + '0';
			carry = 0;
		}
		str[LenTotal - i] = aBit;
	}
	str[LenTotal] = '\0';//end
//	printf("%s", str);
//	str
	for (int i = 0; i < 5; i++)
	{
//		printf("%c",	str[i]);
	}
	return str;
}

int main()
{
	char *rst;
	rst = addBinary("1010", "1011");
	printf("%s", rst);
	for (int i = 0; i < 5; i++)
	{
		printf("%c",	rst[i]);
	}
	return 0;
}
